package com.company.Model.session.question;

import com.company.Model.session.QuestionParam;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Random;
import java.util.Set;


public class QuestionLogic implements Serializable {
    private String currentTopic;
    private Complexity currentComplexity;
    private ArrayList<String> availableTopics;
    private final Complexity[] complexities;

    public QuestionLogic(Set topics) {
        availableTopics = new ArrayList(topics);
        complexities = Complexity.values();
        currentTopic = "JavaEEQuestions.xml";
        currentComplexity = Complexity.medium;
    }

    public QuestionParam getNextParam(boolean result) {
        if (result) {
            currentComplexity = harder();
            currentTopic = randomTopic();
        } else {
            currentComplexity = easier();
        }
        return new QuestionParam(currentTopic, currentComplexity);
    }

    private String randomTopic() {
        return availableTopics.get(new Random().nextInt(availableTopics.size() - 1));
    }

    private Complexity randomComplexity() {
        return complexities[new Random().nextInt(complexities.length - 1)];
    }

    private Complexity harder() {
        if (this.currentComplexity == Complexity.hard) {
            return Complexity.hard;
        }
        return Complexity.values()[this.currentComplexity.ordinal() + 1];
    }

    private Complexity easier() {
        if (this.currentComplexity == Complexity.easy) {
            return Complexity.easy;
        }
        return Complexity.values()[this.currentComplexity.ordinal() - 1];
    }
}
